<?php

namespace App\Http\Controllers\Admin;

use App\Helpers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Repositories;
abstract class AdminController extends Controller
{
    use Helpers\Jump;
    /**
     * 服务仓库
     * @var
     */
    protected $serviceRepository;

    /**
     * AdminController constructor.
     */
    public function __construct()
    {
        $list = (new Repositories\Admin\ConfigRepository())->getConfigList();

        config($list);

        $this->setTemplatePrefix(config('admin.systems.template_prefix', 'admin.'))->setCurrentModule('admin');
    }

    /**
     * 获取当前控制器服务仓库对象
     * @return mixed
     */
    abstract public function getServiceRepository();

    /**
     * 删除数据
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function delete(Request $request)
    {
        if (strtolower($request->method()) == 'get') {
            $ids = $request->route('ids', '');
        } else {
            $ids = $request->input('ids', []);
        }

        if (!is_array($ids)) {
            $ids = explode(',', $ids);
        }

        $re = $this->getServiceRepository()->deleteByIds($ids);

        if (!$re) {
            return $this->error("删除失败");
        }
        return $this->success("删除成功");
    }
}
